import {isExternal} from "@/utils/validate";
import {useRouter} from "vue-router";

export const useGoto=()=>{
    //method
    const router = useRouter()
    const to = (path:string,query?:any)=>{
        if(isExternal(path)){
            window.location.href=path
        }else{
            if(!query)
                router.push(path).catch(err => {
                    console.log(err);
                });
            else
                router.push({path:path,query:query}).catch(err => {
                    console.log(err);
                });
        }
    }
    const replace = (path:string,query?:any)=>{
        if(!query)
            router.replace(path).catch(err => {
                console.log(err);
            });
        else
            router.replace({path:path,query:query}).catch(err => {
                console.log(err);
            });
    }
    const goBack=()=>{
        router.back()
    }
    return {
        to,replace,goBack
    }
}